
Imports System.Reflection
Imports DotNetNuke
Imports DotNetNuke.Entities.Portals

Namespace RTP.HTMLNewsletter
    Public Class HTMLNewsletterEdit
        Inherits NewsletterBase

#Region "Private Members"
        Private _itemId As Integer = -1
        Private _ventrianCategories As New ArrayList
        Private ReadOnly _metaPostAssemblyName As String = Hosting.HostingEnvironment.MapPath(Common.Globals.ApplicationPath + "\bin\") + "RTP.HTMLNewsletter.metaPostProvider.dll"
        Private Const MetaPostClassName As String = "ITCrossing.metaPostHTMLNewsletterProvider"

#End Region

#Region "Event Handlers"

        'This call is required by the Web Form Designer.
        <DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub

        Private Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As EventArgs) Handles MyBase.Load
            ' Determine ItemId of NewsArticle to Update
            If Not (Request.Params("ItemId") Is Nothing) Then
                _itemId = Int32.Parse(Request.Params("ItemId"))
            End If

            If Not Page.IsPostBack Then

                cmdDeleteTemplate.Attributes.Add("onClick", "javascript:return confirm('Are You Sure You Wish To Delete This Template ?');")
                cmdDeleteArchivedNewsletter.Attributes.Add("onClick", "javascript:return confirm('Are You Sure You Wish To Delete This Archived Newsletter ?');")

                Call LoadDDLData()

                Dim arrRoles As ArrayList = GetPortalRoles(PortalId)
                ddlRadRoles.DataSource = arrRoles
                ddlRadRoles.DataBind()
                ddlRadRoles.EmptyMessage = Localization.GetString("EmptyRoleMessage", LocalResourceFile)

                divPublishToCats.Visible = False
                divSummary.Visible = False
                ddlNACats.EmptyMessage = Localization.GetString("None", LocalResourceFile)
                _ventrianCategories = GetVentrianCategories()

                Dim arrVentrianModules As New ArrayList
                Dim objVentrianCategoryInfo As New VentrianCategoryInfo
                objVentrianCategoryInfo.CategoryId = 0
                objVentrianCategoryInfo.CategoryName = Localization.GetString("None", LocalResourceFile)
                objVentrianCategoryInfo.ParentId = 0
                arrVentrianModules.Add(objVentrianCategoryInfo)
                For Each objVentrianCategoryInfo In _ventrianCategories
                    If objVentrianCategoryInfo.ParentId = CategoryNewsArticles Then
                        arrVentrianModules.Add(objVentrianCategoryInfo)
                    End If
                Next

                If arrVentrianModules.Count > 1 Then
                    divPublishTo.Visible = True
                Else
                    divPublishTo.Visible = False
                End If
                ddlNAModules.DataSource = arrVentrianModules
                ddlNAModules.DataBind()
                ddlNAModules.FindItemByValue("0").Selected = True

                txtUserid.Text = GetUserEmail(UserInfo.UserID, Settings)

                Dim blRestrictEmail As Boolean = True
                If CType(Settings("restrictemail"), String) <> "" Then
                    blRestrictEmail = CType(Settings("restrictemail"), Boolean)
                End If
                If blRestrictEmail Then
                    txtUserid.Text = CheckEmail(txtUserid.Text)
                End If

                chkYourself.Checked = False

                If Not _itemId = -1 Then
                    Dim objCtlNewsletter As New NewsletterController
                    Dim objNewsletter As NewsletterInfo = objCtlNewsletter.GetNewsletterbyID(_itemId)
                    If Not objNewsletter Is Nothing Then
                        cmdDeleteArchivedNewsletter.Visible = True
                        Dim content As String = Server.HtmlDecode(objNewsletter.NewsletterBody) ' =====  & "<br><br>Return To <a href='#Top'>Top</a>"
                        ftbDesktopText.Text = content
                        Subject.Text = objNewsletter.NewsletterDesc

                        If objNewsletter.ArticleTabModuleID > 0 Then
                            ddlNAModules.ClearSelection()
                            If Not ddlNAModules.FindItemByValue(NAModToCategory(objNewsletter.ArticleTabModuleID.ToString)) Is Nothing Then
                                ddlNAModules.FindItemByValue(NAModToCategory(objNewsletter.ArticleTabModuleID.ToString)).Selected = True
                                LoadVentrianCategories()
                                TickVentrianCategoryBoxes(objNewsletter.ArticleID, objNewsletter.ArticleTabModuleID)
                            End If
                        End If

                        Dim objNewsletterCategoryCtl As New NewsletterCategoryController
                        ' ReSharper disable RedundantAssignment
                        Dim roleID As Integer = -1
                        ' ReSharper restore RedundantAssignment
                        Dim selectedRole As Integer = -1
                        Dim arrCategories As ArrayList = objNewsletterCategoryCtl.GetNewsletterCategorysByNewsletterID(_itemId)
                        For Each objNewsletterCategory As NewsletterCategoryInfo In arrCategories
                            If objNewsletterCategory.CategoryID = CategoryYourself Then
                                chkYourself.Checked = True
                            ElseIf objNewsletterCategory.CategoryID > CategoryRoles Then
                                roleID = CategoryIDToRole(objNewsletterCategory.CategoryID.ToString)
                                If roleID = -1 Then
                                    Continue For
                                End If
                                For Each item As Telerik.Web.UI.RadComboBoxItem In ddlRadRoles.Items
                                    If item.Value = roleID.ToString Then
                                        selectedRole = roleID
                                        item.Checked = True
                                    End If
                                Next
                            End If
                        Next
                        If selectedRole > -1 Then
                            ddlRadRoles.FindItemByValue(selectedRole.ToString).Selected = True
                        End If

                        ddlPublish.SelectedValue = objNewsletter.Published.ToString()

                        If objNewsletter.NewsletterSummary <> "" Then
                            tbSummary.Text = objNewsletter.NewsletterSummary
                        End If
                    End If
                End If
            End If

            If Page.IsPostBack = False Then
                ' Store URL Referrer to return to portal
                ViewState("UrlReferrer") = "~/DesktopDefault.aspx?tabid=" & TabId
            End If

        End Sub

        Private Sub ddlNAModules_SelectedIndexChanged(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs) Handles ddlNAModules.SelectedIndexChanged
            _ventrianCategories = GetVentrianCategories()
            LoadVentrianCategories()
        End Sub

        Private Sub ddlTemplate_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlTemplate.SelectedIndexChanged
            cmdDeleteArchivedNewsletter.Visible = False
            If ddlTemplate.SelectedItem.Text <> Localization.GetString("None_Selected", LocalResourceFile) Then
                '==== Reset the seleted item on Unsent List and Hide the buttons ====

                cmdSaveTemplate.Visible = True
                cmdDeleteTemplate.Visible = True

                Dim objCtlNewsletterTemplate As New NewsletterTemplateController
                'TLID = ddlTemplate.SelectedItem.Value
                ViewState("TLID") = ddlTemplate.SelectedItem.Value
                Dim objNewsletterTemplate As NewsletterTemplateInfo = objCtlNewsletterTemplate.GetNewsletterTemplatebyID(CInt(ddlTemplate.SelectedItem.Value))
                If Not objCtlNewsletterTemplate Is Nothing Then
                    Dim sContent As String = Server.HtmlDecode(objNewsletterTemplate.TemplateBody)
                    ftbDesktopText.Text = sContent
                End If

            Else
                cmdSaveTemplate.Visible = False
                cmdDeleteTemplate.Visible = False
                ftbDesktopText.Text = ""
                ViewState("TLID") = -1
            End If


        End Sub

        Private Sub cmdCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdCancel.Click
            ' Redirect back to the portal home page
            Response.Redirect(CType(ViewState("UrlReferrer"), String))
        End Sub

        Private Sub cmdSend_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdSend.Click
            Dim publish As Boolean = CType(ddlPublish.SelectedValue, Boolean)
            Dim selectedCategories As New ArrayList
            lblMessage.Visible = False

            Dim objNewsletter As New NewsletterInfo
            If _itemId > -1 Then
                Dim objNewsletterCtl As New NewsletterController
                objNewsletter = objNewsletterCtl.GetNewsletterbyID(_itemId)
            End If

            Dim articleID As Integer = 0
            Dim articleTabModuleID As Integer = 0
            If Not objNewsletter Is Nothing And (CInt(ddlNAModules.SelectedValue) <> 0 Or objNewsletter.ArticleTabModuleID > 0) Then
                Dim objNAArticleInfo As NAArticleInfo = CreateVentrian(objNewsletter.ArticleTabModuleID, objNewsletter.ArticleID)
                If Not objNAArticleInfo Is Nothing Then
                    articleID = objNAArticleInfo.ArticleID
                    articleTabModuleID = objNAArticleInfo.ArticleTabModuleID
                    UpdateVentrianNAImages(objNAArticleInfo.ArticleID)
                End If
            End If

            If _itemId = -1 Then
                _itemId = AddNewsletter(ManageUploadDirectory(tbSummary.Text, PortalSettings.HomeDirectory), ManageUploadDirectory(ftbDesktopText.Text, PortalSettings.HomeDirectory), Subject.Text, UserInfo.UserID, ModuleId, articleID, articleTabModuleID, publish, DateTime.UtcNow(), Settings, PortalId)
            Else
                UpdateNewsletter(_itemId, ManageUploadDirectory(tbSummary.Text, PortalSettings.HomeDirectory), ManageUploadDirectory(ftbDesktopText.Text, PortalSettings.HomeDirectory), Subject.Text, UserInfo.UserID, ModuleId, articleID, articleTabModuleID, publish, DateTime.UtcNow(), Settings)
            End If

            If Not _itemId = -1 Then
                ' ReSharper disable NotAccessedVariable
                Dim result As Boolean = False
                ' ReSharper restore NotAccessedVariable
                Dim addChar As String = ""
                Dim sendToRoles As String = ""

                'Process Roles
                For Each item As Telerik.Web.UI.RadComboBoxItem In ddlRadRoles.CheckedItems
                    selectedCategories.Add(CInt(RoleToCategoryID(CInt(item.Value))))
                    result = SendNewsletterRole(Settings, CType(HttpContext.Current.Items("PortalSettings"), PortalSettings), CInt(item.Value), _itemId, publish, DateTime.UtcNow())
                    sendToRoles += addChar + item.Text
                    addChar = ";"
                Next
                If chkYourself.Checked Then
                    selectedCategories.Add(CategoryYourself)
                    result = SendNewsletterYourself(UserInfo.UserID, UserInfo.Email, _itemId, DateTime.UtcNow())
                End If

                Dim blEnableSocial As Boolean = False
                If CType(Settings("enablesocial"), String) <> "" Then
                    blEnableSocial = CType(Settings("enablesocial"), Boolean)
                End If
                If sendToRoles <> "" And blEnableSocial Then
                    Dim displayTabModule As Integer
                    If CType(Settings("displaytabmodule"), String) <> "" Then
                        displayTabModule = CType(Settings("displaytabmodule"), Integer)
                    Else
                        Dim objModuleController As New Entities.Modules.ModuleController
                        displayTabModule = objModuleController.GetModule(ModuleId, TabId, False).TabModuleID
                    End If

                    Dim objNotifications As New Components.Integration.Notifications
                    objNotifications.NewNewsletterNotification(sendToRoles.Split(";"c), PortalSettings.UserInfo, Subject.Text, _itemId, PortalId, displayTabModule, ModuleId)
                End If

                Dim objNewsletterCategoryCtl As New NewsletterCategoryController
                Dim arrNewsletterCategorys As ArrayList = objNewsletterCategoryCtl.GetNewsletterCategorysByNewsletterID(CInt(_itemId))
                For Each objNewsletterCategory As NewsletterCategoryInfo In arrNewsletterCategorys
                    Dim blDelete As Boolean = True
                    For Each categoryID As Integer In selectedCategories
                        If categoryID = objNewsletterCategory.CategoryID Then
                            blDelete = False
                        End If
                    Next
                    If blDelete Then
                        objNewsletterCategoryCtl.DeleteNewsletterCategory(_itemId, objNewsletterCategory.CategoryID)
                    End If
                Next

                If Not lblMessage.Visible Then
                    ' Redirect back to the portal home page
                    Response.Redirect(CType(ViewState("UrlReferrer"), String))
                End If
            Else
                lblMessage.Text = Localization.GetString("SaveError", LocalResourceFile)
                lblMessage.Visible = True
            End If
        End Sub

        Private Sub cmdSaveTemplateAs_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdSaveTemplateAs.Click
            Dim iTLID As Integer

            Dim objCtlNewsletterTemplate As New NewsletterTemplateController

            iTLID = objCtlNewsletterTemplate.AddNewsletterTemplate(ftbDesktopText.Text, tbTemplateName.Text, PortalId)
            ViewState("TLID") = iTLID
            Call LoadDDLData()
            ddlTemplate.Items.FindByValue(iTLID.ToString).Selected = True
            tbTemplateName.Text = ""
        End Sub

        Private Sub cmdSaveTemplate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdSaveTemplate.Click
            If Not ViewState("TLID") Is Nothing Then
                Dim objCtlNewsletterTemplate As New NewsletterTemplateController
                objCtlNewsletterTemplate.UpdateNewsletterTemplate(ftbDesktopText.Text, CInt(ViewState("TLID")), PortalId)
            End If
        End Sub

        Private Sub cmdDeleteTemplate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdDeleteTemplate.Click
            Dim objCtlNewsletterTemplate As New NewsletterTemplateController
            objCtlNewsletterTemplate.DeleteNewsletterTemplate(CInt(ViewState("TLID")))
            Call LoadDDLData()
            ftbDesktopText.Text = ""
        End Sub

        Private Sub cmdDeleteArchivedNewsletter_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdDeleteArchivedNewsletter.Click
            Dim objNewsletterCtl As New NewsletterController
            Dim objNewsletter As NewsletterInfo = objNewsletterCtl.GetNewsletterbyID(_itemId)
            If Not objNewsletter Is Nothing Then
                If objNewsletter.ArticleID > 0 Then
                    DeleteVentrian(objNewsletter.ArticleID)
                End If
                DeleteNewsletter(_itemId)
            End If
            ftbDesktopText.Text = ""

            Response.Redirect(CType(ViewState("UrlReferrer"), String))
        End Sub


#End Region

#Region "Private Methods "
        Private Sub LoadDDLData()
            '==== Load Template List ====
            Dim objCtlNewsletterTemplate As New NewsletterTemplateController
            Dim drTemplates As ArrayList = objCtlNewsletterTemplate.GetNewsletterTemplates(PortalId)

            ddlTemplate.DataSource = drTemplates
            ddlTemplate.DataTextField = "TemplateName"
            ddlTemplate.DataValueField = "TemplateID"
            ddlTemplate.DataBind()
            ddlTemplate.Items.Insert(0, New ListItem(Localization.GetString("None_Selected", LocalResourceFile), ""))

        End Sub

        Private Sub LoadVentrianCategories()
            ddlNACats.ClearSelection()
            ddlNACats.Text = ""
            Dim arrNACategories As New ArrayList
            If CInt(ddlNAModules.SelectedValue) > 0 Then
                divPublishToCats.Visible = True
                divSummary.Visible = True
                For Each objVentrianCategoryInfo As VentrianCategoryInfo In _ventrianCategories
                    If objVentrianCategoryInfo.ParentId.ToString = ddlNAModules.SelectedValue Then
                        objVentrianCategoryInfo.CategoryName = Trim(Mid(objVentrianCategoryInfo.CategoryName, InStr(objVentrianCategoryInfo.CategoryName, "~") + 1))
                        arrNACategories.Add(objVentrianCategoryInfo)
                    End If
                Next
            Else
                divPublishToCats.Visible = False
                divSummary.Visible = False
            End If
            ddlNACats.DataSource = arrNACategories
            ddlNACats.DataBind()
        End Sub

        Private Sub TickVentrianCategoryBoxes(ByVal articleID As Integer, ByVal articleTabModuleID As Integer)
            Dim arrNACategories As ArrayList = GetVentrianItemCategories(articleID, articleTabModuleID)
            Dim selectedCategory As Integer = -1
            For Each naCategoryID As Integer In arrNACategories
                For Each item As Telerik.Web.UI.RadComboBoxItem In ddlNACats.Items
                    If item.Value = naCategoryID.ToString Then
                        selectedCategory = naCategoryID
                        item.Checked = True
                    End If
                Next
            Next
            If selectedCategory > -1 Then
                ddlNACats.FindItemByValue(selectedCategory.ToString).Selected = True
            End If
        End Sub

        Private Function CheckEmail(ByVal strFrom As String) As String
            Dim userDomain As String
            Dim portalDomain As String = GetDomainURL(CType(HttpContext.Current.Items("PortalSettings"), Entities.Portals.PortalSettings))
            If Left(portalDomain, 4) = "www." Then
                portalDomain = Mid(portalDomain, 5)
            End If

            ' Validate the domain name to try and prevent SPAM - RWJS
            userDomain = Mid(strFrom, InStr(strFrom, "@") + 1)
            ' Use this for Messaging Controller
            ' objMessage.FromUserID = iFromID
            If Right(userDomain, 1) = ">" Then
                userDomain = Left(userDomain, Len(userDomain) - 1)
            End If
            If portalDomain = userDomain Then
                Return strFrom
                'objMessage.ReplyTo = strFrom
            Else
                Return PortalSettings.Email
                'objMessage.ReplyTo = _portalSettings.Email
            End If

        End Function

#End Region

#Region "Functions calling metaPost Provider via reflection"

        Private Function GetVentrianCategories() As ArrayList
            If Not MetaPostAndVentrianInstalled() Then
                Return New ArrayList
            End If

            Dim metaPostAsm As Assembly = Assembly.LoadFrom(_metaPostAssemblyName)
            Dim metaPostType As Type = metaPostAsm.GetType(MetaPostClassName)
            Const methodName As String = "GetVentrianCategories"

            Dim arrTypes As New ArrayList
            arrTypes.Add(UserInfo.GetType())
            arrTypes.Add(PortalSettings.GetType())

            Dim metaPostMethod As MethodInfo = metaPostType.GetMethod(methodName, CType(arrTypes.ToArray(GetType(Type)), Type()))

            Dim metaPostConstructor As ConstructorInfo = metaPostType.GetConstructor(Type.EmptyTypes)
            Dim responder As Object = metaPostConstructor.Invoke(New Object() {})

            Dim returnObj As Object = metaPostMethod.Invoke(responder, New Object() {UserInfo, PortalSettings})
            Dim arrmetaPostCategoryInfo As ArrayList = CType(returnObj, ArrayList)
            Dim arrVentrianCategoryInfo As New ArrayList
            Dim objVentrianCategoryInfo As VentrianCategoryInfo
            For Each objmetaPostVentrianCategoryInfo As Object In arrmetaPostCategoryInfo
                ' ReSharper disable RedundantAssignment
                objVentrianCategoryInfo = New VentrianCategoryInfo
                ' ReSharper restore RedundantAssignment
                objVentrianCategoryInfo = ClassCast(Of VentrianCategoryInfo)(objmetaPostVentrianCategoryInfo)
                arrVentrianCategoryInfo.Add(objVentrianCategoryInfo)
            Next
            Return arrVentrianCategoryInfo
        End Function

        Private Function GetVentrianItemCategories(ByVal articleID As Integer, ByVal articleTabModuleID As Integer) As ArrayList
            If Not MetaPostAndVentrianInstalled() Then
                Return New ArrayList
            End If

            Dim metaPostAsm As Assembly = Assembly.LoadFrom(_metaPostAssemblyName)
            Dim metaPostType As Type = metaPostAsm.GetType(MetaPostClassName)
            Const methodName As String = "GetVentrianItemCategories"

            Dim arrTypes As New ArrayList
            arrTypes.Add(articleID.GetType())
            arrTypes.Add(articleTabModuleID.GetType())
            arrTypes.Add(UserInfo.GetType())
            arrTypes.Add(PortalSettings.GetType())

            Dim metaPostMethod As MethodInfo = metaPostType.GetMethod(methodName, CType(arrTypes.ToArray(GetType(Type)), Type()))

            Dim metaPostConstructor As ConstructorInfo = metaPostType.GetConstructor(Type.EmptyTypes)
            Dim responder As Object = metaPostConstructor.Invoke(New Object() {})

            Dim returnObj As Object = metaPostMethod.Invoke(responder, New Object() {articleID, ArticleTabModuleID, UserInfo, PortalSettings})
            Dim arrVentrianCategories As ArrayList = CType(returnObj, ArrayList)
            Return arrVentrianCategories
        End Function

        ' ReSharper disable UnusedMethodReturnValue.Local
        Private Function DeleteVentrian(ByVal articleID As Integer) As Boolean
            ' ReSharper restore UnusedMethodReturnValue.Local
            If Not MetaPostAndVentrianInstalled() Then
                Return False
            End If

            Dim metaPostAsm As Assembly = Assembly.LoadFrom(_metaPostAssemblyName)
            Dim metaPostType As Type = metaPostAsm.GetType(MetaPostClassName)
            Const methodName As String = "DeleteVentrian"

            Dim arrTypes As New ArrayList
            arrTypes.Add(articleID.GetType())
            arrTypes.Add(UserInfo.GetType())
            arrTypes.Add(PortalSettings.GetType())

            Dim metaPostMethod As MethodInfo = metaPostType.GetMethod(methodName, CType(arrTypes.ToArray(GetType(Type)), Type()))

            Dim metaPostConstructor As ConstructorInfo = metaPostType.GetConstructor(Type.EmptyTypes)
            Dim responder As Object = metaPostConstructor.Invoke(New Object() {})

            Dim returnObj As Object = metaPostMethod.Invoke(responder, New Object() {articleID, UserInfo, PortalSettings})
            Return CType(returnObj, Boolean)
        End Function

        Private Function CreateVentrian(ByVal articleTabModuleID As Integer, ByVal articleID As Integer) As NAArticleInfo
            If Not MetaPostAndVentrianInstalled() Then
                Return Nothing
            End If

            Dim metaPostAsm As Assembly = Assembly.LoadFrom(_metaPostAssemblyName)
            Dim metaPostType As Type = metaPostAsm.GetType(MetaPostClassName)
            Const methodName As String = "CreateVentrian"

            Dim arrNACategories As New ArrayList
            arrNACategories.Add(ddlNAModules.SelectedItem.Text)
            If CInt(ddlNAModules.SelectedValue) > 0 Then
                For Each item As Telerik.Web.UI.RadComboBoxItem In ddlNACats.CheckedItems
                    arrNACategories.Add(ddlNAModules.SelectedItem.Text + " ~ " + item.Text)
                Next
            End If

            Dim arrCategories() As String = CType(arrNACategories.ToArray(GetType(String)), String())
            Dim publish As Boolean = CType(ddlPublish.SelectedValue, Boolean)
            Dim content As String = ManageUploadDirectory(ftbDesktopText.Text, PortalSettings.HomeDirectory)
            Dim summary As String = ManageUploadDirectory(tbSummary.Text, PortalSettings.HomeDirectory)

            Dim arrTypes As New ArrayList
            arrTypes.Add(articleTabModuleID.GetType())
            arrTypes.Add(articleID.GetType())
            arrTypes.Add(arrCategories.GetType())
            arrTypes.Add(content.GetType())
            arrTypes.Add(publish.GetType())
            arrTypes.Add(summary.GetType())
            arrTypes.Add(Subject.Text.GetType())
            arrTypes.Add(UserInfo.GetType())
            arrTypes.Add(PortalSettings.GetType())

            Dim metaPostMethod As MethodInfo = metaPostType.GetMethod(methodName, CType(arrTypes.ToArray(GetType(Type)), Type()))

            Dim metaPostConstructor As ConstructorInfo = metaPostType.GetConstructor(Type.EmptyTypes)
            Dim responder As Object = metaPostConstructor.Invoke(New Object() {})

            Dim returnObj As Object = metaPostMethod.Invoke(responder, New Object() {articleTabModuleID, articleID, arrCategories, content, publish, tbSummary.Text, Subject.Text, UserInfo, PortalSettings})
            Dim objNAArticleInfo As NAArticleInfo
            objNAArticleInfo = ClassCast(Of NAArticleInfo)(returnObj)
            Return objNAArticleInfo
        End Function

        Private Function ClassCast(Of T)(ByVal myobj As [Object]) As T
            Dim target As Type = GetType(T)
            Dim x As Object = Activator.CreateInstance(target, False)
            Dim d As Generic.IEnumerable(Of MemberInfo) = From source In target.GetMembers().ToList() Where source.MemberType = MemberTypes.Property Select source
            Dim members As Generic.List(Of MemberInfo) = d.Where(Function(memberInfo) d.[Select](Function(c) c.Name).ToList().Contains(memberInfo.Name)).ToList()
            Dim propertyInfo As PropertyInfo
            Dim value As Object
            For Each memberInfo As MemberInfo In members
                propertyInfo = GetType(T).GetProperty(memberInfo.Name)
                value = myobj.[GetType]().GetProperty(memberInfo.Name).GetValue(myobj, Nothing)
                propertyInfo.SetValue(x, value, Nothing)
            Next
            Return DirectCast(x, T)
        End Function
#End Region

#Region "NAArticleInfo"
        Public Class NAArticleInfo
            Private _articleTabModuleID As Integer
            Private _articleID As Integer


            Property ArticleTabModuleID() As Integer
                Get
                    Return _ArticleTabModuleID
                End Get
                Set(ByVal value As Integer)
                    _articleTabModuleID = Value
                End Set
            End Property

            Property ArticleID() As Integer
                Get
                    Return _ArticleID
                End Get
                Set(ByVal value As Integer)
                    _articleID = Value
                End Set
            End Property


        End Class
#End Region

#Region "VentrianCategoryInfo"
        Private Class VentrianCategoryInfo
            Private _categoryId As Integer
            Private _categoryName As String
            Private _parentId As Integer

            Public Property CategoryId() As Integer
                ' ReSharper disable UnusedMember.Local
                Get
                    ' ReSharper restore UnusedMember.Local
                    Return _categoryId
                End Get
                Set(ByVal value As Integer)
                    _CategoryId = value
                End Set
            End Property

            Public Property CategoryName() As String
                Get
                    Return _CategoryName
                End Get
                Set(ByVal value As String)
                    _CategoryName = value
                End Set
            End Property

            Public Property ParentId() As Integer
                Get
                    Return _ParentId
                End Get
                Set(ByVal value As Integer)
                    _ParentId = value
                End Set
            End Property

        End Class
#End Region

    End Class
End Namespace